package com.zt.springcloud.service.Impl;

import com.zt.springcloud.RemoteUserService;
import com.zt.springcloud.entity.User;
import com.zt.springcloud.entity.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author RedReins
 * @version 1.0
 * @description: TODO
 * @date 2021/8/28 17:03
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = remoteUserService.getUserInfo(s);
        if(user == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        /*用UserDetail类封装*/
        UserDetail userDetail = new UserDetail();
        userDetail= (UserDetail) user;
        userDetail.setRoles(remoteUserService.getRolesInfo(user.getId()));
        return userDetail;
    }
}
